//
//  RootViewController.m
//  TapToZoom
//
//  Created by heefan on 7/12/11.
//  Copyright 2011 heefan. All rights reserved.
//

#import "RootViewController.h"
#define ZOOM_VIEW_TAG 100
#define ZOOM_STEP 1.5

@interface RootViewController (UtilityMethods)   /* {{{ */ //Private method
	-(CGRect) zoomRectForScale:(float)scale withCenter:(CGPoint)center;
@end
/* }}} */

@implementation RootViewController 
@synthesize imageScrollView, imageView;

/*
 * Reference: UIViewController 
 * Subclassing Notes:
 * When you define a new subclass of UIViewContrller, you must specify the views to be managed by the controller.
 * 2 Ways,
 * (1) Specify  the views manually, you must implement the loadView method and use it to assign a root view object
 *      to the view property. If specify views using a nib file, you must not override loadView
 * (2) Specify the views via Nib file, initialize your view controller object using the initWithNibName:bundle:
 *      method
 */

-(void) loadView  // [prototype] -(void)loadView
{
 	[super loadView];
	
	[imageView setTag:ZOOM_VIEW_TAG]; //TODO ??

	//add gesture recognizers to the image view
	UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self 
										 								action:@selector(handleSingleTap:)];
	UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self
																				action:@selector(handleDoubleTap:)];
	UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self 
																				action:@selector(handleTwoFingerTap:)];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
	[imageScrollView release];
	[imageView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
} #pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark -
#pragma mark Utility methods

-(CGRect) zoomRectForScale:(float)scale withCenter:(CGPoint) center
{ /* {{{ */
	/*
	 * Reference : UIView 
	 *
	 * struct CGRect {
	 * 		CGPoint origin;
	 * 		CGSize  size;
	 * };
	 */
	CGRect zoomRect;
	zoomRect.size.height = [imageScrollView frame].size.height / scale;
	zoomRect.size.width = [imageScrollView frame].size.width / scale;
	
	zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0);
	zoomRect.origin.y = center.y - (zoomRect.size.height/2.0);

	return zoomRect;
} /* }}} */

@end
